package com.example.youjingjing.electronicbalance.widge;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.internal.NavigationMenuView;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.youjingjing.electronicbalance.R;
import com.example.youjingjing.electronicbalance.adapter.MyFragmentStatePagerAdapter;
import com.example.youjingjing.electronicbalance.fragment.FirstFragment;
import com.example.youjingjing.electronicbalance.fragment.SecondFragment;
import com.example.youjingjing.electronicbalance.presenter.MainPresenter;
import com.example.youjingjing.electronicbalance.view.MainView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends BaseActivity<MainView,MainPresenter> implements MainView,NavigationView.OnNavigationItemSelectedListener {


    @BindView(R.id.main_viewpager)
    ViewPager viewpager;

    @BindView(R.id.toolbar)
    Toolbar toolbar;


    @BindView(R.id.main_rootLayout)
    CoordinatorLayout rootLayout;

    @BindView(R.id.draw_layout)
    DrawerLayout drawerLayout;

    protected ActionBarDrawerToggle actionBarDrawerToggle;

    @BindView(R.id.navigation_view)
    NavigationView navigationView;

    TextView textView_user;
    ImageView imageView_user;

    @BindView(R.id.toolbar_tabLayout)
    TabLayout mTabLayout;

    List<Fragment> fragments;
    String[] title = {"tab1", "tab2"};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        setSupportActionBar(toolbar);


        setTitle("首页");
        //设置左上角的图标响应
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.open, R.string.close);
        actionBarDrawerToggle.syncState();

        navigationView.setNavigationItemSelectedListener(this);
        disableNavigationViewScrollbars(navigationView);


//        View headerView = navigationView.inflateHeaderView(R.layout.navigation_header);
        View headerView = navigationView.getHeaderView(0);
        View iv = headerView.findViewById(R.id.nav_iv_user);
        imageView_user = (ImageView) headerView.findViewById(R.id.nav_iv_user);
        textView_user = (TextView) headerView.findViewById(R.id.nav_tv_user);
        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jumpActivity(LoginActivity.class);
            }
        });

        autoLogin();
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        if(extras!=null){
            String name = extras.getString("name");
//            psw =  extras.getString("psw");
            textView_user.setText(name); }

        fragments = new ArrayList<Fragment>();
        FirstFragment f = new FirstFragment();
        SecondFragment s = new SecondFragment();
        fragments.add(f);
        fragments.add(s);

        MyFragmentStatePagerAdapter adapter = new MyFragmentStatePagerAdapter(getSupportFragmentManager(),fragments,title);
        viewpager.setAdapter(adapter);
        mTabLayout.setupWithViewPager(viewpager);



    }
    @Override
    public void showUserInfo() {
        String name =readData("name");
        String psw =readData("psw");
        textView_user.setText(name);
    }

    @Override
    public void autoLogin() {
        if(readBoolean("AUTO_ISCHECK")){
            showUserInfo();
        }
    }
    @Override
    protected MainPresenter createPresenter() {
        return new MainPresenter();
    }

    @OnClick(R.id.main_fabBtn)
    public void onClick(){
        Snackbar.make(rootLayout, "Hello. I am Snackbar!", Snackbar.LENGTH_SHORT)
                .setAction("Undo", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                    }
                })
                .show();
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        /*关闭侧滑菜单*/
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.draw_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
    private long mExitTime;
    @Override
    public void onBackPressed() {

        if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
            drawerLayout.closeDrawer(GravityCompat.START);
        } else {
            if ((System.currentTimeMillis() - mExitTime) > 2000) {
                Toast.makeText(this,"再按一次退出程序",Toast.LENGTH_SHORT).show();
                mExitTime = System.currentTimeMillis();
            } else {
                finish();
            }
        }
    }
    //设置选中menuItem打开抽屉
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == android.R.id.home)
        {
            drawerLayout.openDrawer(GravityCompat.START);
            return true ;
        }
        return super.onOptionsItemSelected(item);
    }

    //去掉navigation里的scroll
    private void disableNavigationViewScrollbars(NavigationView navigationView) {
        if (navigationView != null) {
            NavigationMenuView navigationMenuView = (NavigationMenuView) navigationView.getChildAt(0);
            if (navigationMenuView != null) {
                navigationMenuView.setVerticalScrollBarEnabled(false);
            }
        }
    }


    /*当屏幕旋转或者配置改变的时候，我们需要保存当前的状态。*/
//    @Override
//    public void onSaveInstanceState(Bundle outState) {
//        super.onSaveInstanceState(outState);
//        outState.putInt(POSITION,mTabLayout.getSelectedTabPosition());
//    }
//
//    @Override
//    protected void onRestoreInstanceState(Bundle savedInstanceState) {
//        super.onRestoreInstanceState(savedInstanceState);
//        viewpager.setCurrentItem(savedInstanceState.getInt(POSITION));
//    }
}
